package com.xhwl.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.logistics.dao.mapper.IVehicleRelationCarToCardMapper;
import com.xhwl.logistics.entity.VehicleRelationCarToCard;

import java.util.Date;
import java.util.List;
import java.util.Optional;

//@Service
public class VehicleRelationCarToCardServiceImpl extends ServiceImpl<IVehicleRelationCarToCardMapper, VehicleRelationCarToCard> {

    private Wrapper<VehicleRelationCarToCard> conditionForQueryRelation(Integer carId) {
        LambdaQueryWrapper<VehicleRelationCarToCard> queryWrapper = Wrappers.lambdaQuery();
        return queryWrapper
                .eq(VehicleRelationCarToCard::getCarId, carId);
    }

    private Wrapper<VehicleRelationCarToCard> conditionForQueryRelations(List<Integer> carIdList) {
        LambdaQueryWrapper<VehicleRelationCarToCard> queryWrapper = Wrappers.lambdaQuery();
        return queryWrapper
                .in(VehicleRelationCarToCard::getCarId, carIdList);
    }

    private Wrapper<VehicleRelationCarToCard> conditionForDeleteRelation(Integer id, Integer deleterAccountId, String deleterAccountName) {
        LambdaUpdateWrapper<VehicleRelationCarToCard> updateWrapper = Wrappers.lambdaUpdate();
        return updateWrapper
                .eq(VehicleRelationCarToCard::getId, id)
                .set(VehicleRelationCarToCard::getDeleted, 1)
                .set(VehicleRelationCarToCard::getDeleterAccountId, deleterAccountId)
                .set(VehicleRelationCarToCard::getDeleterAccountName, deleterAccountName)
                .set(VehicleRelationCarToCard::getUpdatedAt, new Date());
    }

    private Wrapper<VehicleRelationCarToCard> conditionForDeleteRelations(List<Integer> idList) {
        LambdaUpdateWrapper<VehicleRelationCarToCard> updateWrapper = Wrappers.lambdaUpdate();
        return updateWrapper
                .in(VehicleRelationCarToCard::getId, idList)
                .set(VehicleRelationCarToCard::getDeleted, 1)
                .set(VehicleRelationCarToCard::getDeleterAccountName, "xxl-job")
                .set(VehicleRelationCarToCard::getUpdatedAt, new Date());
    }

    public Optional<VehicleRelationCarToCard> queryRelation(Integer carId) {
        List<VehicleRelationCarToCard> vehicleRelationCarToCardList = list(conditionForQueryRelation(carId));
        if (vehicleRelationCarToCardList.isEmpty()) {
            return Optional.empty();
        } else if (vehicleRelationCarToCardList.size() == 1) {
            return Optional.ofNullable(vehicleRelationCarToCardList.get(0));
        } else {
            throw new BusinessException("出现异常，无法删除，同一个车辆具有多个车卡的绑定关系");
        }
    }

    public List<VehicleRelationCarToCard> queryRelations(List<Integer> carIdList) {
        return list(conditionForQueryRelations(carIdList));
    }

    public void deleteRelation(Integer id, Integer deleterAccountId, String deleterAccountName) {
        update(conditionForDeleteRelation(id, deleterAccountId, deleterAccountName));
    }

    public void deleteRelations(List<Integer> idList) {
        update(conditionForDeleteRelations(idList));
    }

}
